package org.jeecg.modules.flow.callback.delivery.move;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTask;
import org.jeecg.modules.business.delivery.service.IWmsDeliveryTaskService;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryDocumentVO;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryVO;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.entity.vo.DistributeLocationDetailVO;
import org.jeecg.modules.business.transfer.entity.vo.DistributeLocationVO;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptMaterialService;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.move.util.CommonMoveUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 条料调拨因入移库单 分配库位
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-11-27
 */
@RequiredArgsConstructor
@Component
public class Y103MoveReceiptDistributeLocationBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsMoveReceiptService moveReceiptService;

    private final IWmsMoveReceiptMaterialService moveReceiptMaterialService;

    private final IWmsDeliveryTaskService deliveryTaskService;

    private final IWmsInReceiptTaskService inReceiptTaskService;

    private final IWmsInReceiptTaskMaterialService inReceiptTaskMaterialService;

    private final CommonMoveUtil commonMoveUtil;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String infoId = baseFlowDto.getInfoId();
        baseFlowDto.translateClass(DistributeLocationVO.class);
        DistributeLocationVO distributeLocationVO = (DistributeLocationVO) baseFlowDto.getData();
        if(StrUtil.isEmpty(distributeLocationVO.getMoveReceiptId())){
            throw new JeecgBootException("移库单编号不能为空");
        }
        WmsMoveReceipt moveReceipt = moveReceiptService.getById(distributeLocationVO.getMoveReceiptId());
        if(Objects.isNull(moveReceipt)){
            throw new JeecgBootException("单据不存在");
        }
        if(!moveReceipt.getDocumentType().equals("Y103")){
            throw new JeecgBootException("单据类型错误");
        }
        if(CollectionUtil.isEmpty(distributeLocationVO.getDetailVOList())){
            throw new JeecgBootException("库位信息不能为空");
        }
        // 判断移入库区库位是否为空
        List<DistributeLocationDetailVO> detailVOList = distributeLocationVO.getDetailVOList();
        boolean flag = detailVOList.stream().allMatch(o -> {
            return StrUtil.isEmpty(o.getWmsBasicWarehouseId())
                    || StrUtil.isEmpty(o.getWmsBasicWarehouseAreaId())
                    || StrUtil.isEmpty(o.getWmsBasicWarehouseAreaLocationId());
        });
        if(flag){
            throw new JeecgBootException("移入库区、移入库位不能为空");
        }

        // 更新即时库存的库位信息
        List<WmsMoveReceiptMaterial> moveReceiptMaterialList=new ArrayList<>();
        for(DistributeLocationDetailVO detailVO: distributeLocationVO.getDetailVOList()){
            WmsMoveReceiptMaterial moveReceiptMaterial = moveReceiptMaterialService.getById(detailVO.getMoveReceiptMaterialId());
            moveReceiptMaterial.setTransferToAreaId(detailVO.getWmsBasicWarehouseAreaId());
            moveReceiptMaterial.setTransferToLocationId(detailVO.getWmsBasicWarehouseAreaLocationId());
            moveReceiptMaterialList.add(moveReceiptMaterial);
        }
        commonMoveUtil.moveRepertory(moveReceipt.getId(),moveReceipt.getTransferFromWarehouseId(),moveReceiptMaterialList);
        // 更新移库单物料明细位置
        moveReceiptMaterialService.updateBatchById(moveReceiptMaterialList);
        // 更新入库任务物料明细的已移库字段
//        WmsDeliveryTask wmsDeliveryTask = deliveryTaskService.getById(moveReceipt.getDeliveryTaskId());
//        WmsInReceiptTask inReceiptTask = inReceiptTaskService.getOne(new LambdaQueryWrapper<WmsInReceiptTask>()
//                .eq(WmsInReceiptTask::getDocumentCode, wmsDeliveryTask.getInTaskCode())
//        );
//        for(DistributeLocationDetailVO detailVO: distributeLocationVO.getDetailVOList()){
//            inReceiptTaskMaterialService.update(new LambdaUpdateWrapper<WmsInReceiptTaskMaterial>()
//                    .set(WmsInReceiptTaskMaterial::getHasMoved,1)
//                    .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId,inReceiptTask.getId())
//                    .eq(WmsInReceiptTaskMaterial::getBatchCode,detailVO.getBatchCode())
//                    .eq(WmsInReceiptTaskMaterial::getWmsBasicMaterialId,detailVO.getWmsBasicMaterialId())
//                    .eq(WmsInReceiptTaskMaterial::getWmsBasicWarehouseId,detailVO.getWmsBasicWarehouseId())
//            );
//        }
        //判断是否移库单物料是否都有库位了
        List<WmsMoveReceiptMaterial> updatedList = moveReceiptMaterialService.list(new LambdaQueryWrapper<WmsMoveReceiptMaterial>()
                .eq(WmsMoveReceiptMaterial::getWmsMoveReceiptId, distributeLocationVO.getMoveReceiptId())
        );
        boolean allMatch = updatedList.stream().allMatch(o -> !StrUtil.isEmpty(o.getTransferToLocationId()));
        if(allMatch){
            //都有库位了让节点流转
            baseFlowDto.setRunThenStop(false);
        }else{
            //没有库位不让节点流转
            baseFlowDto.setRunThenStop(true);
        }
        baseFlowDto.setInfoId(infoId);
        baseFlowDto.setResult(Result.OK());
    }

}
